package com.swap.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.swap.business.config.CustomizeException;
import com.swap.business.domain.SwapDevice;
import com.swap.business.dto.DeviceDto;
import com.swap.business.dto.SnmpMessageDTO;
import com.swap.business.service.DeviceDataAcquisitionService;
import com.swap.business.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/snmpAcquisition")
public class SnmpDataAcquisitionController {

    @Autowired
    private DeviceDataAcquisitionService deviceDataAcquisitionService;

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/getAll")
    public DeviceDto getAll(@RequestBody SnmpMessageDTO snmpInfo) throws JsonProcessingException, CustomizeException {
        SwapDevice swapDevice = new SwapDevice();
        DeviceDto dto = new DeviceDto();
        QueryWrapper<SwapDevice> query = Wrappers.query();
        query.eq("status", 1);
        query.orderByAsc("id");
        List<SwapDevice> list = deviceService.list(query);
        for (SwapDevice div : list) {
            deviceDataAcquisitionService.queryGetAllDeviceInfo(snmpInfo, div);
        }

        return dto;
    }


    @PostMapping("/getOne")
    public void getOne(@RequestBody SnmpMessageDTO snmpInfo) throws JsonProcessingException, CustomizeException {
        QueryWrapper<SwapDevice> query = Wrappers.query();
        query.eq("device_ip", snmpInfo.getIp());
        query.eq("community_read", snmpInfo.getCommunity());
        query.eq("version", snmpInfo.getVersion());
        query.eq("status", 1);
        SwapDevice one = deviceService.getOne(query);

        deviceDataAcquisitionService.queryGetAllDeviceInfo(snmpInfo, one);
    }

}
